﻿using System;
using System.Windows;
using System.Xml;
using System.Text;

using System.Windows.Documents;
using System.Windows.Threading;
using System.Xml.Linq;

using TJDevHouse.Starcraft.Game;
using TJDevHouse.Starcraft.AIModuleConnector.ImportExport;
using TJDevHouse.Starcraft.AIModuleConnector;

namespace TJDevHouse.Starcraft.MapAnalyser
{

	

	/// <summary>
	/// Interaction logic for MapAnalysisWindow.xaml
	/// </summary>
	public partial class MapAnalysisWindow : Window
	{

		private DispatcherTimer _gameRunningChecker;

		public Map CurrentMap { get; private set; }

		private MapDrawing.MapDisplay BasicDataMapDisplay;
		private MapDrawing.MapDisplay WalkabilityMapDisplay;
		private MapDrawing.MapDisplay PathFindingMapDisplay;

		public MapAnalysisWindow()
		{
			InitializeComponent();

			//check whether there is an actual game running, to enable the Load Game Map menu item as needed
			_gameRunningChecker = new DispatcherTimer();
			_gameRunningChecker.Tick += new EventHandler(TickGameRunningChecker);
			_gameRunningChecker.Interval = TimeSpan.FromMilliseconds(1000);
			_gameRunningChecker.Start();

			

		}

		private void TickGameRunningChecker(object source, EventArgs e)
		{

			if (Connector.Connection.IsGameRunning)
			{
				menuItem_UseInGameMap.IsEnabled = true;
			}
			
		}



		/// <summary>
		/// Change the map to analyse
		/// </summary>
		/// <param name="map"></param>
		private void ChangeMap(Map map)
		{
			//basic data map
			BasicDataMapDisplay = new MapDrawing.MapDisplay(map, canvas_BasicData);
			
			MapDrawing.MapLayer terrainLayer = new MapDrawing.TerrainMapLayer();
			MapDrawing.MapLayer startingPositionsLayer = new MapDrawing.StartingPositionsMapLayer();
			MapDrawing.MapLayer staticResourcesLayer = new MapDrawing.StaticResourcesMapLayer();
			
			BasicDataMapDisplay.AddMapLayer(terrainLayer);
			BasicDataMapDisplay.AddMapLayer(startingPositionsLayer);
			BasicDataMapDisplay.AddMapLayer(staticResourcesLayer);

			//walkability map
			WalkabilityMapDisplay = new MapDrawing.MapDisplay(map, canvas_WalkableZones);
			MapDrawing.MapLayer walkabilityLayer = new MapDrawing.WalkabiltiyZonesMapLayer();
			WalkabilityMapDisplay.AddMapLayer(walkabilityLayer);

			//pathfinding map
			PathFindingMapDisplay = new MapDrawing.MapDisplay(map, canvas_pathFindingTests);
			MapDrawing.MapLayer pathfindingTerrainLayer = new MapDrawing.TerrainMapLayer();
			MapDrawing.MapLayer pathfinginTestsLayer = new MapDrawing.PathfindingTestsMapLayer();
			PathFindingMapDisplay.AddMapLayer(pathfindingTerrainLayer);
			PathFindingMapDisplay.AddMapLayer(pathfinginTestsLayer);


			//render the maps
			BasicDataMapDisplay.Render();
			WalkabilityMapDisplay.Render();
			PathFindingMapDisplay.Render();

		}

		/// <summary>
		/// Specify the map to use by specifying its mapData.xml file
		/// </summary>
		/// <param name="mapDataFilePath"></param>
		private void ChangeMap(string mapDataFilePath)
		{
			Map newMap = MapDataStorageController.CreateMap(mapDataFilePath);
			ChangeMap(newMap);
		}

		private void menuItem_Open_Click(object sender, RoutedEventArgs e)
		{
			Microsoft.Win32.OpenFileDialog browseDialog = new Microsoft.Win32.OpenFileDialog();
			browseDialog.CheckPathExists = true;
			browseDialog.DefaultExt = "mapData.txt";
			browseDialog.FileName = "mapData.txt";
			browseDialog.Multiselect = false;
			
			browseDialog.Title = "Select Map Data File";

			bool? res = browseDialog.ShowDialog();

			if (true == res)
			{
				ChangeMap(browseDialog.FileName);
			}

		}

		private void menuItem_Save_Click(object sender, RoutedEventArgs e)
		{

		}

		private void menuItem_Close_Click(object sender, RoutedEventArgs e)
		{
			this.Close();
		}

		private void menuItem_UseInGameMap_Click(object sender, RoutedEventArgs e)
		{
			//save off the map data as needed
			Map map = Connector.Connection.Game.Map;
			//MapDataStorageController.GetMapData(map);
			ChangeMap(map);
		}

		private void menuItem_Refresh_Click(object sender, RoutedEventArgs e)
		{
			BasicDataMapDisplay.Render();
			WalkabilityMapDisplay.Render();
			PathFindingMapDisplay.Render();
		}

	}
}
